#include "sensorworker.h"
#include <QThread>
#include <QDateTime>

void SensorWorker::clearInputEdge()
{
    MCF_Clear_Input_Fall_Bit_Net(m_di, 0);
}

void SensorWorker::setOutputBit(ushort state)
{
    MCF_Set_Output_Bit_Net(m_do, state, 0);
}

void SensorWorker::setTimedOutput(ushort state, ushort duration)
{
    MCF_Set_Output_Time_Bit_Net(m_do, state, duration, 0);
}

bool SensorWorker::checkInputEdge(ushort &edgeCount)
{
    short rtn = MCF_Get_Input_Fall_Bit_Net(m_di, &edgeCount, 0);
    return (rtn == 0 && edgeCount > 0);
}

bool SensorWorker::checkInputLevel(ushort &level)
{
    short rtn = MCF_Get_Input_Bit_Net(m_di, &level, 0);
    return (rtn == 0);
}

long SensorWorker::getEncoderCount()
{
    long count = 0;
    MCF_Get_Encoder_Net(0, &count, 0);
    return count;
}

void SensorWorker::waitForEncoderCount(long targetCount)
{
    long currentCount = 0;
    while (currentCount < targetCount && m_running) {
        MCF_Get_Encoder_Net(0, &currentCount, 0);
        QThread::msleep(1);
    }
}
